﻿using NUnit.Framework;
using System;
using SyringeControl.Factories;
using SyringeControl.AssemblySelectors;
using System.Reflection;
using System.Windows.Forms;

namespace SyringeControl.Tests
{
    class FakeView : IControlView
    {
        #region Члены IControlView

        public event System.EventHandler AssemblySelectionRequested;

        public event System.EventHandler ControlReloadRequested;

        public event System.EventHandler ControlSetupRequested;

        #endregion

        internal void RequestAssemblySelection()
        {
            if (AssemblySelectionRequested != null)
                AssemblySelectionRequested(this, EventArgs.Empty);
        }

        internal void RequestControlReload()
        {
            if (ControlReloadRequested != null)
                ControlReloadRequested(this, EventArgs.Empty);
        }

        internal void RequestControlSetup()
        {
            if (ControlSetupRequested != null)
                ControlSetupRequested(this, EventArgs.Empty);
        }

    }

    [TestFixture]
    public class Test_MainFormController
    {
        FakeView view = new FakeView();
        ViewControlModel model = new ViewControlModel();
        IControlFactory controlFactory = new ActivatorControlFactory();
        IAssemblySelector assemblySelector = new PathAssemblySelector(Assembly.GetExecutingAssembly().Location);
        MainFormController controller;

        [SetUp]
        public void CreateController()
        {
            controller = new MainFormController(model, view, controlFactory, assemblySelector);
        }


        [Test]
        public void Sets_selected_assembly_on_request()
        {
            view.RequestAssemblySelection();
            Assert.That(model.SelectedAssembly, Is.Not.Null);
        }

        [Test]
        public void Loads_control_types_when_selected_assembly_changes()
        {
            model.SelectedAssembly = assemblySelector.SelectAssembly();
            Assert.That(model.Types, Has.Count.GreaterThan(0));
        }

        [Test]
        public void Instantiates_control_when_model_selected_value_is_set()
        {
            model.SelectedType = typeof(TextBox);
            Assert.That(model.TestedControl, Is.InstanceOf<TextBox>());
        }
    }
}
